package com.handinfo.uimenu.window;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameMIDlet;
import com.handinfo.engine.GameManager;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UIAuctionSearch extends UIAbstractWindow
{
	public static byte ORDER_MONEY_ASC = 0 ;
	public static byte ORDER_MONEY_DES = 1 ;
	public static byte ORDER_TIME_ASC = 2 ;
	public static byte ORDER_TIME_DES = 3 ;
	
	private final int H_PRE = UITools.H_LABEL_STR + UITools.IH_RECTS;
	private final String TITLE = "我要购买";
	//
	private final String[] TYPE = new String[] { "全部", "装备", "消耗", "材料", "其他" };
	private final String[] CAREER = new String[] { "全部", "剑客", "武者", "道士" };
	private final String[] COLOR = new String[] { "全部", "灰", "白", "蓝", "绿", "金", "紫", "橙", "红" };
	private final String[] POS = new String[] { "全部", "武器", "头", "称号", "护符", "腰", "衣", "饰品", "鞋", "裤" };
	private final String[] MARTERIAL = new String[] { "全部", "普通材料", "功能材料", "宝石", "天工坊", "八卦炉" };

	private final String ATT_TYPE = "类型:";
	private final String ATT_CAREER = "职业:";
	private final String ATT_COLOR = "稀有程度:";
	private final String ATT_LEVEL = "物品等级:";
	private final String ATT_POS = "部位:";
	private final String ATT_MARTERIAL = "材料图样:";
	private final String ATT_ITEMNAME = "物品名称:";
	//
	private final int INDEX_TYPE = 0;
	private final int INDEX_CAREER = 1;
	private final int INDEX_COLOR = 2;
	private final int INDEX_LEVEL_MIN = 3;
	private final int INDEX_LEVEL_MAX = 4;
	private final int INDEX_POS = 5;
	private final int INDEX_MARTERIAL = 6;
	private final int INDEX_ITEMNAME = 7;

	private final int COLOR_ATT = UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS);
	private final int W_INPUT = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1)
			- AbstractGameScreen.s_font.stringWidth(ATT_ITEMNAME) - UITools.IW_RECTS;;
	private final int W_INPUT_LEVEL_INTER = 10;
	private final int W_INPUT_LEVEL = (W_INPUT - W_INPUT_LEVEL_INTER) >> 1;
	private final int H_DES = (UITools.ENDY_CONTENT - UITools.INITY_CONTENT) - UITools.IH_RECTS - 6 * H_PRE;

	private String m_content_type = "";
	private String m_content_career = "";
	private String m_content_color = "";
	private String m_content_level_min = "";
	private String m_content_level_max = "";
	private String m_content_pos = "";
	private String m_content_marterial = "";
	private String m_content_itemName = "";

	private int[] m_indexs;
	private int m_index_career = 0;
	private int m_index_color = 0;
	private int m_index_pos = 0;
	private int m_index_marterial = 0;

	private String[] m_des ;

	public UIAuctionSearch(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		m_state = -1;
		setWindowStatus(0);
		setWindowIndex(INDEX_TYPE);
	}

	public static void test()
	{
		UIAuctionSearch s = new UIAuctionSearch(-1);
		UIManager.getInstance().addWindow(s);
	}
	
	private void sendData(){
		int[] ID_TYPE = new int[] { 0, 1, 2, 3, 5 };
		int[] ID_CAREER = new int[] { 100, 1, 2, 3 };
		int[] ID_COLOR = new int[] { 100, 0, 1, 2, 3, 4, 5, 6, 7 };
		int[] ID_POS = new int[] { 100, 0, 1, 2, 3, 4,5, 6, 8, 9 };
		int[] ID_MARTERIAL = new int[] { 100, 1, 2, 3, 4, 5 };
		
		GameManager.getInstance().m_game_message.sendAuctionSearch(
				0, 0, UIAuctionList.MAXROW_ITEMS,UIAuctionSearch.ORDER_MONEY_ASC,
				ID_TYPE[m_state],
				ID_MARTERIAL[m_index_marterial],
				ID_COLOR[m_index_color],
				m_content_level_min.equals("") ? 0 : Integer.parseInt(m_content_level_min),
				m_content_level_max.equals("") ? 0 : Integer.parseInt(m_content_level_max),
				ID_CAREER[m_index_career],
				ID_POS[m_index_pos],
				"",
				m_content_itemName);
	}
	
	
	
	protected void setWindowIndex(int index_new)
	{
		super.setWindowIndex(index_new);
		
		String str =null ;
		switch (m_index) {
			case INDEX_COLOR:
				str = "点击选择物品品质"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break;
			case INDEX_CAREER:
				str = "点击选择使用职业"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break ;
			case INDEX_MARTERIAL:
				str = "点击选择材料图样"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break;
			case INDEX_POS:
				str = "点击选择装备部位"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break;
			case INDEX_TYPE:
				str = "点击选择物品大类"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break;
			case INDEX_ITEMNAME:
				str = "点击输入名称查找"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break ;
			case INDEX_LEVEL_MIN:
			case INDEX_LEVEL_MAX:
				str = "点击输入物品等级"+Tools.CHANGE_ROW + "确定开始搜索" ;
				break;
		}
		m_des = Tools.getStrArray(str, UITools.W_RECT
				- (UIManager.getColorsSubcoatBorder().length << 1));
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			sendData() ;
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			int index = getPosIndex();
			if (index == -1) {
				return;
			}
			if (index == 0) {
				setWindowIndex(m_indexs[m_indexs.length - 1]);
			}
			else {
				setWindowIndex(m_indexs[index - 1]);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			int index = getPosIndex();
			if (index == -1) {
				return;
			}
			if (index == m_indexs.length - 1) {
				setWindowIndex(m_indexs[0]);
			}
			else {
				setWindowIndex(m_indexs[index + 1]);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
			if (m_index == INDEX_LEVEL_MIN) {
				setWindowIndex(INDEX_LEVEL_MAX);
			}
			else if (m_index == INDEX_LEVEL_MAX) {
				setWindowIndex(INDEX_LEVEL_MIN);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			String[] menus = null;
			switch (m_index) {
				case INDEX_COLOR:
					menus = COLOR;
					break;
				case INDEX_CAREER:
					menus = CAREER;
					break;
				case INDEX_MARTERIAL:
					menus = MARTERIAL;
					break;
				case INDEX_POS:
					menus = POS;
					break;
				case INDEX_TYPE:
					menus = TYPE;
					break;
				case INDEX_ITEMNAME:
				case INDEX_LEVEL_MIN:
				case INDEX_LEVEL_MAX:
					gotoFrom();
					break;
			}
			if (menus == null) {
				return;
			}
			final UIPopmenu menu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
			menu.setPopmenus(menus);
			menu.addListener(new UIListener() {

				public void actionPerformed()
				{
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						int index = menu.getCurrentIndex();
						switch (m_index) {
							case INDEX_COLOR:
								changeColorIndex(index);
								break;
							case INDEX_CAREER:
								changeCareerIndex(index);
								break;
							case INDEX_MARTERIAL:
								changeMarterialIndex(index);
								break;
							case INDEX_POS:
								changePosIndex(index);
								break;
							case INDEX_TYPE:
								setWindowStatus(index);
								break;
						}
						UIManager.getInstance().removeWindow(menu);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(menu);
					}
				}
			});
		}
	}

	private int getPosIndex()
	{
		if (m_indexs == null) {
			return -1;
		}
		for (int i = 0, len = m_indexs.length; i < len; i++) {
			if (m_indexs[i] == m_index) {
				return i;
			}
			if (m_index >= INDEX_LEVEL_MAX && m_indexs[i] == INDEX_LEVEL_MIN) {
				return i;
			}
		}
		return -1;
	}

	protected void actionOnStateChanged()
	{
		m_content_type = TYPE[m_state];
		switch (m_state) {
			case 1://装备
				m_indexs = new int[] { INDEX_TYPE, INDEX_POS, INDEX_COLOR, INDEX_LEVEL_MIN, INDEX_CAREER,
						INDEX_ITEMNAME };
				break;
			case 3://材料
				m_indexs = new int[] { INDEX_TYPE, INDEX_MARTERIAL, INDEX_COLOR, INDEX_LEVEL_MIN, INDEX_ITEMNAME };
				break;
			default:
				m_indexs = new int[] { INDEX_TYPE, INDEX_COLOR, INDEX_LEVEL_MIN, INDEX_ITEMNAME };
				break;
		}
		changeCareerIndex(0);
		changeColorIndex(0);
		changePosIndex(0);
		changeMarterialIndex(0);
		
		registerTouch();
	}

	private void changeCareerIndex(int index)
	{
		m_index_career = index;
		m_content_career = CAREER[m_index_career];
	}

	private void changeColorIndex(int index)
	{
		m_index_color = index;
		m_content_color = COLOR[m_index_color];
	}

	private void changePosIndex(int index)
	{
		m_index_pos = index;
		m_content_pos = POS[m_index_pos];
	}

	private void changeMarterialIndex(int index)
	{
		m_index_marterial = index;
		m_content_marterial = MARTERIAL[m_index_marterial];
	}

	private void gotoFrom()
	{
		Form myForm = new Form("请输入搜索条件");
		final TextField textField2 = new TextField("请输入最小等级", m_content_level_min, 2, TextField.NUMERIC);
		final TextField textField3 = new TextField("请输入最大等级", m_content_level_max, 2, TextField.NUMERIC);
		final TextField textField4 = new TextField("请输入物品名称", m_content_itemName, 8, TextField.ANY);
		myForm.append(textField2);
		myForm.append(textField3);
		myForm.append(textField4);
		final StringItem warnInfoItem = new StringItem("", "");
		myForm.append(warnInfoItem);
		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d)
			{
				if (c == ok) {
					int num1 = 0;
					int num2 = 0;
					//
					if (!textField2.getString().equals("")) {
						try {
							num1 = Integer.parseInt(textField2.getString());
						}
						catch (Exception e) {
							warnInfoItem.setText("只能输入数字");
							textField2.setString(m_content_level_min);
							return;
						}
						if (num1 < 0) {
							warnInfoItem.setText("最小等级需为正数");
							textField2.setString(m_content_level_min);
							return;
						}
					}
					if (!textField3.getString().equals("")) {
						try {
							num2 = Integer.parseInt(textField3.getString());
						}
						catch (Exception e) {
							warnInfoItem.setText("只能输入数字");
							textField3.setString(m_content_level_max);
							return;
						}
						if (num2 < 0) {
							warnInfoItem.setText("最大等级需为正数");
							textField3.setString(m_content_level_max);
							return;
						}
					}
//					if (num2 < num1) {
//						warnInfoItem.setText("最大等级需大于或等于最小等级");
//						return;
//					}
					String itemName = textField4.getString();
					//
					if (num1 == 0) {
						m_content_level_min = "";
					}
					else {
						m_content_level_min = String.valueOf(num1);
					}
					if (num2 == 0) {
						m_content_level_max = "";
					}
					else {
						m_content_level_max = String.valueOf(num2);
					}
					m_content_itemName = itemName;
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
				else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}

		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	/**************************************************/

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE);
		int tempX = m_x + UITools.INITX_CONTENT + UITools.IW_RECT_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT + UITools.IH_RECT_CONTENT;
		int tempW = UITools.W_CONTENT;
		int tempH = H_PRE;
		if (m_indexs != null) {
			for (int i = 0, len = m_indexs.length; i < len; i++) {
				switch (m_indexs[i]) {
					case INDEX_COLOR:
						paintRow(g, tempX, tempY, tempW, tempH, ATT_COLOR, m_content_color, INDEX_COLOR);
						break;
					case INDEX_CAREER:
						paintRow(g, tempX, tempY, tempW, tempH, ATT_CAREER, m_content_career, INDEX_CAREER);
						break;
					case INDEX_ITEMNAME:
						paintRow(g, tempX, tempY, tempW, tempH, ATT_ITEMNAME, m_content_itemName, INDEX_ITEMNAME);
						break;
					case INDEX_MARTERIAL:
						paintRow(g, tempX, tempY, tempW, tempH, ATT_MARTERIAL, m_content_marterial, INDEX_MARTERIAL);
						break;
					case INDEX_POS:
						paintRow(g, tempX, tempY, tempW, tempH, ATT_POS, m_content_pos, INDEX_POS);
						break;
					case INDEX_TYPE:
						paintRow(g, tempX, tempY, tempW, tempH, ATT_TYPE, m_content_type, INDEX_TYPE);
						break;
					case INDEX_LEVEL_MIN:
					case INDEX_LEVEL_MAX:
						paintLevel(g, tempX, tempY, tempW, tempH);
						break;
				}
				tempY += H_PRE;
			}
		}
		paintDes(g);
	}

	private void paintLevel(Graphics g, int x, int y, int w, int h)
	{
		g.setColor(COLOR_ATT);
		g.drawString(ATT_LEVEL, x, y, 0);
		x = x + w - W_INPUT_LEVEL;
		UITools.paintInputRect(g, x, y, W_INPUT_LEVEL, h, m_content_level_max, m_index == INDEX_LEVEL_MAX);
		x -= W_INPUT_LEVEL_INTER;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS)) ;
		g.drawLine(x+1, y+(h>>1), x + W_INPUT_LEVEL_INTER-2, y+(h>>1));
		x -= W_INPUT_LEVEL;
		UITools.paintInputRect(g, x, y, W_INPUT_LEVEL, h, m_content_level_min, m_index == INDEX_LEVEL_MIN);
	}

	private void paintDes(Graphics g)
	{
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.ENDY_CONTENT - H_DES - UITools.IH_RECTS;
		UITools.paintContentRect(g, tempX, tempY, UITools.W_RECT, H_DES);
		if (m_des == null) {
			return;
		}
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		for (int i = 0; i < m_des.length; i++) {
			g.drawString(m_des[i], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
		}
	}

	private void paintRow(Graphics g, int x, int y, int w, int h, String att, String content, int index)
	{
		g.setColor(COLOR_ATT);
		g.drawString(att, x, y, 0);
		UITools.paintInputRect(g, x + w - W_INPUT, y, W_INPUT, UITools.H_LABEL_STR, content, m_index == index);
	}

	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		int[][]rect = null;
		switch (m_state) {
		case 1://装备
			rect = new int[][]{
					{77,42,380,21},
					{77,67,380,21},
					{77,91,380,21},
					{77,117,184,22},
					{272,117,184,22},
					{77,142,380,21},
					{117,167,380,21},
			};
			break;
		case 3://材料
			rect = new int[][]{
					{77,42,380,21},
					{77,67,380,21},
					{77,91,380,21},
					{77,117,184,22},
					{272,117,184,22},
					{77,142,380,21},
			};
			break;
		default:
			rect = new int[][]{
					{77,42,380,21},
					{77,67,380,21},
					{77,92,184,22},
					{272,92,184,22},
					{117,42,380,21},
			};
			break;
		}
		initTouchRect(rect);
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		if (touchid == getPosIndex()) {
			GameTouchManager.keyPressedFire();
		} else {
//			if (touchid < m_indexs.length) {
//				if (touchid > 1 && m_indexs[touchid - 1] == INDEX_LEVEL_MIN) {
//					setWindowIndex(INDEX_LEVEL_MAX);
//				} else {
			setWindowIndex(m_indexs[touchid]);
//				}
//			}
		}
	}
	
	
}
